package com.ai.utils;

import com.ai.exception.BusinessException;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CaptchaUtils {

    public static void outCaptcha(HttpServletRequest request, HttpServletResponse response) {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.setLen(2);
        try {
            CaptchaUtil.out(captcha, request, response);
        } catch (Exception e) {
            throw new BusinessException("验证码异常");
        }
    }

    public static void verCaptcha(HttpServletRequest request, String captcha) {
        //判断验证码
        if (!CaptchaUtil.ver(captcha, request)) {
            // 清除session中的验证码
            CaptchaUtil.clear(request);
            throw new BusinessException("验证码错误！");
        }
    }


}
